import { ref, reactive, onMounted } from 'vue'
import { EditType } from '@/type/BaseEnum'
import useInstance from '@/hooks/useInstance'
import { useRouter,useRoute } from 'vue-router'
import {AddressListType} from "@/api/address/AddressModel"
import { getListApi } from '@/api/address'
 
 
export default function useAddress() {
	//新增弹框的ref属性
	const addRef = ref<{ show: (type: string, row?: any) => void }>()
	const router = useRouter()
	const route = useRoute()
	//获取全局属性
	const { global } = useInstance()
	//新增
	const addBtn = () => {
		addRef.value?.show(EditType.ADD)
	}
	//编辑
	const editBtn = (row: any) => {
		addRef.value?.show(EditType.EDIT, row)
	}
	const dataList = ref<AddressListType[]>([]);
	const getList =   async () => {
	 
		let res:any = await  getListApi({
			page: 1,
			pagesize: 2000
		}) 
		if(res&&res.code==0){
		  dataList.value = res.result.list;
		}
	 
	}
	const reFresh =()=>{
		 
		getList()
		
		if(route.query.needBack as any == 1){
			setTimeout(()=>{
				router.go(-1)
			},500)
		}
		
	}
	onMounted(() => {
		  getList()
	})
	return {
		getList,
		addRef,
		addBtn,
		editBtn,
		dataList,
		reFresh
	}
}
